package com.maiyuren.fishkit.history

import com.maiyuren.fishkit.utils.FishStorage
import org.json.JSONArray
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.Collections
import java.util.Date
import java.util.Vector


class HistoryManager private constructor() {
    var words_ = Vector<String?>()
    var preNextWords_ = Vector<String?>()
    private var currPos_ = -1
    private val s_cfg_item_words = "history_words"
    private val MAX_COUNT = 200
    init {
        loadHistory()
    }

    private fun loadHistory() {
        try {
            val js = FishStorage.getJson(FishStorage.keys.history_words)
            if (js == null) return
            val words_js = js.getJSONArray(s_cfg_item_words)
            if (words_js != null && words_js.length() > 0) {
                for (i in 0 until words_js.length()) {
                    val word_itemStr = words_js.getString(i)
                    words_.add(word_itemStr)
                }
                preNextWords_.addAll(words_)
                Collections.reverse(preNextWords_)
                currPos_ = words_.size
                if (currPos_ == 0) {
                    currPos_ = -1
                }
            }
        } catch (ex: Exception) {
        }
    }

    fun clear() {
        words_.clear()
        preNextWords_.clear()
        currPos_ = -1
        saveConfig()
    }

    fun addWord(word: String) {
        var found = true
        while (words_.size >= MAX_COUNT) {
            words_.removeAt(words_.size - 1)
        }
        while (found) {
            found = false
            for (i in words_.indices) {
                if (words_.elementAt(i) == word) {
                    words_.removeAt(i)
                    found = true
                    break
                }
            }
        }
        words_.add(0, word)
        var preNextToRemoveCount = preNextWords_.size - currPos_ - 1
        while (preNextToRemoveCount > 0) {
            preNextWords_.removeAt(currPos_ + preNextToRemoveCount)
            --preNextToRemoveCount
        }
        if (preNextWords_.size > 0) {
            if (preNextWords_.elementAt(preNextWords_.size - 1) != word) {
                preNextWords_.add(word)
            }
        } else {
            preNextWords_.add(word)
        }
        currPos_ = preNextWords_.size - 1
        if (currPos_ < 0) currPos_ = 0
        saveConfig()
    }

    fun nextWord(): String? {
        if (preNextWords_.size == 0 || currPos_ >= preNextWords_.size - 1) return null
        ++currPos_
        return preNextWords_.elementAt(currPos_)
    }

    fun preWord(): String? {
        if (preNextWords_.size == 0 || currPos_ <= 0) return null
        --currPos_
        return preNextWords_.elementAt(currPos_)
    }

    private fun saveConfig() {
        try {
            val js_root = JSONObject()
            if (words_.size > 0) {
                val words_js = JSONArray()
                for (i in words_.indices) {
                    val item = words_.elementAt(i)
                    words_js.put(item)
                }
                js_root.put(s_cfg_item_words, words_js)
                FishStorage.setJson(FishStorage.keys.history_words, js_root)
            } else {
                FishStorage.setJson(FishStorage.keys.history_words, null)
            }
        } catch (ex: Exception) {
        }
    }

    companion object {
        private var self_: HistoryManager? = null
        fun instance(): HistoryManager {
            if (self_ == null) {
                self_ = HistoryManager()
            }
            return self_!!
        }
    }
}
